package cn.com.dcsgo.controller;

import cn.com.dcsgo.common.Result;
import cn.com.dcsgo.constants.CoreConstant;
import cn.com.dcsgo.enums.ResultEnums;
import cn.com.dcsgo.utils.ServletUtils;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * CaptchaController
 *
 * @author Dcsgo
 * @since 20/5/2025 下午 2:27
 */
@Slf4j
@RestController
@Tag(name = "验证码")
public class CaptchaController {
    @Operation(summary = "获取验证码")
    @GetMapping(CoreConstant.CAPTCHA_URL)
    public Result<String> getCaptcha() {
        // 生成验证码 width height codeCount lineCount
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100, 1, 100);
        // 正确的验证码
        String code = lineCaptcha.getCode();
        // 把正确的验证码放到session中
        ServletUtils.getSession().setAttribute(CoreConstant.CAPTCHA_KEY, code);
        // 把验证码转换成一个图片的字符串
        String imageBase64Data = lineCaptcha.getImageBase64Data();
        log.debug("getCaptcha():{}", code);
        return new Result<>(ResultEnums.SUCCESS, imageBase64Data);
    }
}
